﻿//devicetreewindow.js

/*****************************************************
* k-phu: 2013/11/22
* Manage/Admin
*****************************************************/

var tabs;
var root;
var treenode;
function admin() {
    root = new Ext.tree.AsyncTreeNode({
        id: '1',
        iconCls: 'root',
        loader: new Ext.tree.TreeLoader({
            dataUrl: "HandlerGetTreeJson",
            listeners: {
                "beforeload": function(treeloader, node) {
                    treeloader.baseParams = {
                        id: node.id,
                        method: 'POST'
                    };
                }
            },
            timeout:60000
        })
    });

    var hiddenPkgs = [];
    var filter = new Ext.tree.TreeFilter(treenode, {
        clearBlank: true,
        autoClear: true
    });
    function filterTree(e) {

        var text = e.target.value;
        Ext.each(hiddenPkgs, function(n) {
            n.ui.show();
        });
        if (!text) {
            filter.clear();
            return;
        }

        var re = new RegExp('^' + Ext.escapeRe(text), 'i');


        // hide empty packages that weren't filtered
        hiddenPkgs = [];
        treenode.root.cascade(function(n) {

            if (hasChild(n, re) == false) {
                n.ui.hide();
                hiddenPkgs.push(n);
            }
        });
        function hasChild(n, re) {

            var str = false;

            n.cascade(function(n1) {
                var text, name = "", account = "";
                if (n1.text != undefined) {
                    text = n1.text;
                    if (text.indexOf('(') < 0) {
                        name = text;
                        account = text;
                    }
                    else {
                        name = text.substring(0, text.indexOf('('));
                        account = text.substring(text.indexOf('(') + 1, text.length - 1);
                    }
                }
                if (re.test(name) || re.test(account)) {
                    str = true;
                    return;
                }
            });
            return str;
        }

    }
    var searchbar = [
		 '_TrackerMain_Search',
		 ' ',
		{
		    xtype: 'textfield',
		    width: 150,
		    id: 'message',
		    name: 'message',

		    listeners: {
		        render: function(f) {

		            f.el.on('keydown', filterTree, f, { buffer: 350 });
		        }
		    }
		}, { xtype: 'tbfill' }, {
		    tooltip: '_TrackerMain_Expand',
		    pressed: true,
		    iconCls: 'Expand',
		    handler: function() {
		        treenode.root.expand(true);
		    }
		}, '-', {
		    tooltip: '_TrackerMain_Collpase',
		    pressed: true,
		    iconCls: 'Collpase',
		    handler: function() {
		        treenode.root.collapse(true);
		    }
    }]

    treenode = new Ext.tree.TreePanel({
        autoScroll: true,
        draggable: false,
        animate: true,
        root: root,
        border: false,
        lines: true,
        enableDD: false,
        rootVisible: false,
        containerScroll: true,
        tbar: searchbar,
        listeners: {
            "click": function(node, event) {
                ALLEvents(node);
            }
        }
    });

    // if(!isOpenExtentFn)
        // treenode.expandAll();
    tabs = new Ext.TabPanel({
        style: "padding:0 5px 0 5px",
        region: "center",
        enableTabScroll: true,
        layoutOnTabChange: true,
        items: []
		//,        plugins: new Ext.ux.TabCloseMenu()
    });

    var nav = new Ext.Panel({
       // title: _NewForText_Navigation,
        autoScroll: true,
        region: 'west',
        split: true,
        width: 250,
        collapsible: true,
        margins: '3 0 3 3',
        cmargins: '3 3 3 3',
        items: [treenode]
    });

    var win = new Ext.Window({
        //title: _TrackerMain_Manager,
        closable: true,
        width: 700,
        height: 550,
        plain: true,
        layout: 'border',
        constrain:true,
        items: [nav, tabs]
    });

    win.show();

};